package com.rcw.commonlib.fragment;


import android.os.Build;
import android.os.Bundle;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rcw.commonlib.R;
import com.rcw.popup.FilterBean;
import com.rcw.popup.LabelGridLayout;
import com.rcw.popup.SingleFilterBean;
import com.rcw.popup.TypeLabelGridLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MulFilterFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MulFilterFragment extends Fragment implements View.OnClickListener{

    private static final String SINGLE_DATA="SINGLE_DATA";
    private static final String MUL_DATA="MUL_DATA";

    private TextView tvConfirm,tvReset,tvDes;
    private LabelGridLayout mGridLayout;
    private TypeLabelGridLayout mTypeGridLayout;

    private List<SingleFilterBean> singleData;
    private List<FilterBean> mulData;


    public MulFilterFragment() {
    }

    public static MulFilterFragment newInstance(List<SingleFilterBean> singleData,List<FilterBean> mulData) {
        MulFilterFragment fragment = new MulFilterFragment();
        Bundle args = new Bundle();
        args.putParcelableArrayList(SINGLE_DATA, (ArrayList<SingleFilterBean>) singleData);
        args.putParcelableArrayList(MUL_DATA, (ArrayList<FilterBean>) mulData);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            singleData = getArguments().getParcelableArrayList(SINGLE_DATA);
            mulData = getArguments().getParcelableArrayList(MUL_DATA);
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view =inflater.inflate(R.layout.fragment_mul_filter, container, false);
        EventBus.getDefault().register(this);
        initView(view);

        return view;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void initView(View view) {
        TextView tvTitle= (TextView) view.findViewById(R.id.tv_title);
        tvConfirm=(TextView)view.findViewById(R.id.tv_confirm);
        tvReset=(TextView)view.findViewById(R.id.tv_reset);
        tvDes=(TextView)view.findViewById(R.id.tv_des);
        tvConfirm.setOnClickListener(this);
        tvReset.setOnClickListener(this);
        //流布局数据展示控件
        mGridLayout = (LabelGridLayout) view.findViewById(com.rcw.filterpopup.R.id.gl_label);
        mTypeGridLayout = (TypeLabelGridLayout) view.findViewById(com.rcw.filterpopup.R.id.gl_type_label);

        setViewData();

        tvTitle.setOnClickListener(this);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void setViewData() {
        mTypeGridLayout.setColumnCount(3);
        mTypeGridLayout.setLabelBg(R.drawable.flow_popup);
        mTypeGridLayout.setMulEnable(false);
        mTypeGridLayout.setTitleColor(getResources().getColor(R.color.colorFont));
        mTypeGridLayout.setGridData(mulData);

        mGridLayout.setColumnCount(3);
        mGridLayout.setLabelBg(R.drawable.flow_popup);
        mGridLayout.setMulEnable(true);
        mGridLayout.setGridData(singleData);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_title:
                EventBus.getDefault().post(new EventMessage(1));
                break;
            case R.id.tv_confirm:
                Log.e("rcw","selectSingle="+mGridLayout.getLabelData());
                Log.e("rcw","selectMul="+mTypeGridLayout.getLabelData());
                EventBus.getDefault().post(new EventMessage(-1));
                break;
            case R.id.tv_reset:
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mTypeGridLayout.resetData();
                        mGridLayout.resetData();
                    }
                });

                break;
        }
    }

    // 普通事件的处理
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handleData(String data) {
        //String data=message.getData();
        Log.e("rcw","data==="+data);
        tvDes.setText(data);
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this); //解除注册
        super.onDestroy();

    }

}
